#include "ConsoleSettings.h"
#include <iostream>

ConsoleSettings::ConsoleSettings(unsigned char type, void * handle)
{
	this->handle = handle;
	this->type = type;

	csbi.cbSize = sizeof(this->csbi);

	Get();
}

bool ConsoleSettings::Get()
{
	
	if(type == INPUT)
	{
		codepage = GetConsoleCP();
		if(!GetConsoleMode(handle, &mode))
			return false;
	}
	else
	{
		codepage = GetConsoleOutputCP();
		if(!GetConsoleMode(handle, &mode) || !GetConsoleCursorInfo(handle, &cursor) || !GetConsoleScreenBufferInfoEx(handle, &csbi))
			return false;
	}
	return true;
}

bool ConsoleSettings::Set()
{
	if(type == INPUT)
	{
		if(!SetConsoleCP(codepage) || !SetConsoleMode(handle, mode))
			return false;
	}
	else
	{
		if(!SetConsoleOutputCP(codepage) || !SetConsoleMode(handle, mode) || !SetConsoleCursorInfo(handle, &cursor) || !SetConsoleScreenBufferInfoEx(handle, &csbi) || !SetConsoleWindowInfo(handle, true, &csbi.srWindow))
		{
			std::cout << GetLastError() << std::endl;
			return false;
		}
	}
	return true;
}